﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Feicui.BeautyCRM.IDAL;
using System.Data.Entity;
using System.Linq.Expressions;
using System.Data;

namespace Feicui.BeautyCRM.DAL
{
    /// <summary>
    /// 增删改查操作基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseDal<T> where T : class, new()
    {
        /// <summary>
        /// 获取BeautyContainer的实例
        /// </summary>
        public DbContext Db
        {
            get { return DbContextFactory.GetCurrentDbContext(); }
        }

        #region  查询

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="wherelambda">条件</param>
        /// <returns></returns>
        public IQueryable<T> QueryEntitys(Expression<Func<T, bool>> wherelambda)
        {
            return Db.Set<T>().Where(wherelambda)
                .AsQueryable();
        }

        /// <summary>
        /// 分页方法
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize">行数</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="totle">总数</param>
        /// <param name="wherelambda">条件</param>
        /// <param name="ordlambda">排序</param>
        /// <param name="isAsc">是否正序</param>
        /// <returns></returns>
        public IQueryable<T> QueryPageEntitys<S>(int pageSize, int pageIndex, out int totle, Expression<Func<T, bool>> wherelambda, Expression<Func<T, S>> ordlambda, bool isAsc = true)
        {
            totle = Db.Set<T>().Count();
            if (isAsc)
            {
                return Db.Set<T>().Where(wherelambda)
                    .OrderBy(ordlambda)
                    .Skip(pageSize * (pageIndex - 1))
                    .Take(pageSize)
                    .AsQueryable();
            }
            else
            {
                return Db.Set<T>().Where(wherelambda)
                    .OrderByDescending(ordlambda)
                    .Skip(pageSize * (pageIndex - 1))
                    .Take(pageSize)
                    .AsQueryable();
            }
        }

        #endregion

        #region  新增

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T AddEntity(T entity)
        { 
            Db.Set<T>().Add(entity);
            return entity;
        }
         
        #endregion

        #region  修改

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool EditEntity(T entity)
        {
            Db.Entry(entity).State = EntityState.Modified;
            return true;
        }
        #endregion

        #region  删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DelEntity(T entity)
        {
            Db.Entry(entity).State = EntityState.Deleted;
            return true;
        }
        #endregion
    }
}
